package com.example.final_project.Comm.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.final_project.Comm.Activity.ContentActivity;
import com.example.final_project.Comm.entry.CommContent;
import com.example.final_project.Comm.entry.CommDatas;
import com.example.final_project.Comm.entry.CommGuild;
import com.example.final_project.R;

import java.util.ArrayList;
import java.util.List;

public class CommAdapter extends RecyclerView.Adapter {

    private Context context;
    private CommDatas datas;
    private List<CommGuild> guilds;
    private List<CommContent> contents;
    public static final int GUILD = 0;
    public static final int CONTENT = 1;

    public CommAdapter(Context context, CommDatas list){
        this.context = context;
        this.datas = list;
        this.guilds = datas.getGuilds();
        List<CommContent> list1 = new ArrayList<>();
        list1 = datas.getContents();
        this.contents = list1;
    }

    public void getData(List<CommContent> list){
        contents.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case GUILD:
                View comm = LayoutInflater.from(context).inflate(R.layout.comm_position,parent,false);
                holder = new GuildHolder(comm);
                break;
            case CONTENT:
                View view = LayoutInflater.from(context).inflate(R.layout.comm_content_position,parent,false);
                holder = new ContentHolder(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        int type = getItemViewType(position);
        switch (type){
            case GUILD:
                GuildHolder guildHolder = (GuildHolder)holder;
                guildHolder.setGuild();
                break;
            case CONTENT:
                ContentHolder contentHolder = (ContentHolder)holder;
                contentHolder.setContent();
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        switch (position){
            case GUILD:
                type=GUILD;
                break;
            case CONTENT:
                type=CONTENT;
                break;
        }
        return type;
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    //导航Holder
    class GuildHolder extends RecyclerView.ViewHolder {
        public GuildHolder(@NonNull View itemView) {
            super(itemView);
        }
        public void setGuild(){
            RecyclerView comm = itemView.findViewById(R.id.comm_guild);
            GuildAdapter guildAdapter = new GuildAdapter(context,guilds);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
            comm.setAdapter(guildAdapter);
            comm.setLayoutManager(gridLayoutManager);
        }
    }

    //评论Holder
    class ContentHolder extends RecyclerView.ViewHolder {
        public ContentHolder(@NonNull View itemView) {
            super(itemView);
        }
        public void setContent(){
            RecyclerView content = itemView.findViewById(R.id.comm_content_position);
            ContentAdapter contentAdapter = new ContentAdapter(context,contents);
            content.setAdapter(contentAdapter);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
            content.setLayoutManager(linearLayoutManager);
        }
    }

}
